package com.dg.river.module.common.dialog.error;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dg.river.R;
import com.dg.river.core.util.TextUtil;
import com.dg.river.core.util.Utils;

/**
 * @author jrj
 * @description 标准含标题dialog
 * @update 2022/11/09
 * @update jrj 2022/11/18 规范代码的方法名，变量名等
 */
public class NormalMessageDialog extends Dialog {

    private TextView tvConfirm;
    private TextView tvCancel;
    private TextView tvTitle;
    private TextView tvMessage;

    private Context context;
    private String title;
    private String message;
    private String confirmBtnText;
    private int confirmBtnColor;
    private String cancelBtnText;
    private int cancelBtnColor;
    private boolean cancel = false;
    private OnClickListener confirmBtnClickListener;
    private OnClickListener cancelBtnClickListener;
    private static final String IS_SET_GRAY = "true";

    private NormalMessageDialog dialog;

    public NormalMessageDialog(Context context) {
        super(context);
        this.context = context;
    }

    public NormalMessageDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }

    /**
     * 设置是否能够取消
     *
     * @param bool
     * @return
     */
    public NormalMessageDialog setCancle(boolean bool) {
        this.cancel = bool;
        return this;
    }

    /**
     * Set the Dialog title from String
     *
     * @param title
     * @return
     */
    public NormalMessageDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    /**
     * Set the Dialog message from String
     *
     * @param message
     * @return
     */
    public NormalMessageDialog setMessage(String message) {
        this.message = message;
        return this;
    }

    /**
     * Set the positive button and it's listener
     *
     * @param mConfirmBtnText
     * @return
     */
    public NormalMessageDialog setConfirmButton(String mConfirmBtnText, int mConfirmBtnColor, OnClickListener listener) {
        this.confirmBtnText = mConfirmBtnText;
        this.confirmBtnColor = mConfirmBtnColor;
        this.confirmBtnClickListener = listener;
        return this;
    }

    /**
     * Set the negative button and it's listener
     *
     * @param
     * @return
     */
    public NormalMessageDialog setCancelButton(String mCancelBtnText, int mCancelBtnColor, OnClickListener listener) {
        this.cancelBtnText = mCancelBtnText;
        this.cancelBtnColor = mCancelBtnColor;
        this.cancelBtnClickListener = listener;
        return this;
    }

    /**
     * 传入0为默认样式 否则为自定义样式
     *
     * @param style
     * @return
     */
    public NormalMessageDialog create(int style) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (style == 0) {
            dialog = new NormalMessageDialog(context, R.style.dialog_message_normal);
        } else {
            dialog = new NormalMessageDialog(context, style);
        }
        View layout = inflater.inflate(R.layout.dialog_normal_message_permission, null);
        dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);

        tvConfirm = layout.findViewById(R.id.tv_confirm);
        tvCancel = layout.findViewById(R.id.tv_cancel);
        tvTitle = layout.findViewById(R.id.tv_title);
        tvMessage = layout.findViewById(R.id.tv_message);
        /**
         * 设置显示
         */
        tvTitle.setText(TextUtil.isEmptyConvert(title));
        tvMessage.setText(TextUtil.isEmptyConvert(message));
        tvConfirm.setText(confirmBtnText);
        tvCancel.setText(cancelBtnText);
        if (cancelBtnColor != -1) {
            tvCancel.setTextColor(Utils.getColor(context, cancelBtnColor));
        }
        if (confirmBtnColor != -1) {
            tvConfirm.setTextColor(Utils.getColor(context, confirmBtnColor));
        }
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmBtnClickListener != null) {
                    confirmBtnClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                }
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cancelBtnClickListener != null) {
                    cancelBtnClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                }
            }
        });
        dialog.setContentView(layout);
        if (!cancel) {
            dialog.setCancelable(false);
        } else {
            dialog.setCancelable(true);
        }
        return dialog;
    }
}
